package com.wgh.product.service.impl;

import com.wgh.product.entity.shop.Sku;
import com.wgh.product.mapper.SkuMapper;
import com.wgh.product.service.SkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.math.BigDecimal;

/**
 * SKU Service实现类
 * 
 * @author AI Shop
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Override
    public List<Sku> getSkuListByProductId(Long productId) {
        return baseMapper.selectByProductId(productId);
    }

    @Override
    public Sku getSkuByCode(String skuCode) {
        return baseMapper.selectBySkuCode(skuCode);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStockBatch(List<Long> skuIds, Integer stock) {
        return baseMapper.updateStockBatch(skuIds, stock) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePriceBatch(List<Long> skuIds, Double price) {
        BigDecimal bigDecimalPrice = BigDecimal.valueOf(price);
        return baseMapper.updatePriceBatch(skuIds, bigDecimalPrice) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatusBatch(List<Long> skuIds, Integer status) {
        return baseMapper.updateStatusBatch(skuIds, status) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deductStock(Long skuId, Integer quantity) {
        return baseMapper.deductStock(skuId, quantity) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addStock(Long skuId, Integer quantity) {
        return baseMapper.addStock(skuId, quantity) > 0;
    }

    @Override
    public Sku getSkuDetail(Long id) {
        return baseMapper.selectSkuDetailById(id);
    }

    @Override
    public List<Sku> getSkuListByProductIds(List<Long> productIds) {
        return baseMapper.selectByProductIds(productIds);
    }
}